package pt.isec.gps.fastnote;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FileManager {
    private static final String FAST_NOTE = "/FastNote";

    public String generateFileName() {
        makeFolder();
        String folderName = MessageFormat.format("{0}{1}", System.getProperty("user.home"), FAST_NOTE);
        String fileName = MessageFormat.format("{0}/{1}.txt", folderName, new Date().getTime());
        File ficheiro = new File(fileName);
        while (ficheiro.exists()) {
            fileName = MessageFormat.format("{0}/{1}.txt", folderName, new Date().getTime());
            ficheiro = new File(fileName);
        }

        return fileName;
    }

    private void makeFolder() {
        String folderName = System.getProperty("user.home") + FAST_NOTE;
        File folder = new File(folderName);
        if (!folder.exists()) {
            folder.mkdir();
        }
    }

    public boolean save(Map<String, Note> allNotes) {
        makeFolder();
        try {
            for (Note nota : allNotes.values()) {
                if (nota.getContent() != null) {
                    removeNote(nota.getFileName());
                    File f = new File(nota.getFileName());
                    f.createNewFile();
                    BufferedWriter ficheiro = new BufferedWriter(new FileWriter(nota.getFileName()));
                    ficheiro.write(nota.getContent());
                    ficheiro.flush();
                    ficheiro.close();
                }
            }
        }

        catch (Exception e) {

            return false;
        }
        return true;
    }

    public void removeNote(String fileName) {
        File f = new File(fileName);
        if (f.exists()) {
            f.delete();
        }
    }

    public Map<String, Note> restoreAllNotes() {
        Map<String, Note> allNotes = new HashMap<String, Note>();
        makeFolder();
        File f = new File(System.getProperty("user.home") + FAST_NOTE);
        String[] allFiles = f.list();
        for (int i = 0; i < allFiles.length; i++) {
            Note note = getConteudo(allFiles[i]);
            allNotes.put(note.getFileName(), note);
        }
        return allNotes;
    }

    private Note getConteudo(String nomeFicheiro) {
        BufferedReader f = null;
        String folderFile = System.getProperty("user.home") + FAST_NOTE + "/" + nomeFicheiro;
        Note notaSaida = new Note(folderFile, "");
        try {
            f = new BufferedReader(new FileReader(folderFile));
        } catch (Exception e) {
        }
        String stringXpto;
        try {
            do {
                stringXpto = f.readLine();
                if (stringXpto != null)
                    notaSaida.setContent(notaSaida.getContent() + stringXpto);
            } while (stringXpto != null);
        } catch (IOException e) {
        } catch (NullPointerException e) {
        }
        try {
            f.close();
        } catch (IOException e) {
        }
        return notaSaida;
    }

}
